    /*
 * Copyright (c) 2022, yifengling0. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. No personal names or organizations' names associated with the
 *    Atomthreads project may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE ATOMTHREADS PROJECT AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#include <Arduino.h>
#include <string.h>
#include "atom.h"
#include "debug.h"
#include "syslog.h"
#define ATOM_NONE_STANDARDIO
#include "drv_adc.h"

#ifdef BSP_USE_ADC1
    static Esp32AdcChannelConfig adc1_config[] = {
        { 0, 1},
        { 1, 2},
        { 2, 3},
        { 3, 4},
        { 4, 5},
        { 5, 6},
        { 6, 7},
    };
#endif

#ifdef BSP_USE_ADC2
    static const Esp32AdcChannelConfig adc2_config[] = {
        { 11, 1 }, 
        { 12, 2},
        { 13, 3},
        { 15, 4},
        { 16, 5},
        { 17, 6},
        { 18, 7},
    };
#endif

static Esp32AdcConfig _config[] = {
#ifdef BSP_USE_ADC1    
    { adc1_config, ITEM_NUM(adc1_config), "adc1"},
#endif   
#ifdef BSP_USE_ADC2    
    { adc2_config, ITEM_NUM(adc1_config), "adc2" },
#endif
};

#ifdef BSP_USE_ADC1    
    static Esp32Adc adc1;
#endif   
#ifdef BSP_USE_ADC2    
    static Esp32Adc adc2;
#endif

static Esp32Adc * _adc_list[] = {
#ifdef BSP_USE_ADC1    
    &adc1,
#endif  
#ifdef BSP_USE_ADC2    
    &adc2,
#endif 
};

static int find_io( const Esp32AdcConfig *config, int channel )
{
    int i;
    for( i = 0; i < config->num; i++){
        if( config->config[i].channel == channel ){
            return config->config[i].gpio;
        }
    }

    return -1;
}

static int adc_read(FD fd, char* buffer, size_t maxbytes)
{
    Esp32Adc* adc = (Esp32Adc*)fd;

    AdcValue* value = (AdcValue*)buffer;

    if (maxbytes == sizeof(AdcValue)) {
        int index = find_io(adc->Protected.config, value->channel);
        if( index != -1){
            int result = analogRead(value->channel);
            value->value = result;
            return OK;
        }
    }

    return NG;
}

static int adc_ioctl(FD fd, int function, void* arg)
{
    return 0;
}

static AdcOps _ops = {
    .refVol = 330,
    .convBits = 1 << 12,
    adc_read,
    adc_ioctl
};

void Esp32AdcCreate()
{
    for (size_t i = 0; i < ITEM_NUM(_adc_list); i++)
    {
        Esp32Adc *adc = _adc_list[i];
        adc->Protected.config = &_config[i];
        AdcCreate(&adc->super, _config[i].name, &_ops);
        SYSLOG(LOG_INFO, "adc module %s initlized", _config[i].name);
    }
}
